package com.jiang.books.repository;

import java.math.BigInteger;
import java.util.Date;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;

import com.jiang.books.domain.Book;

public interface BookRepository extends PagingAndSortingRepository<Book, BigInteger> {
	
	public Book findByTitle(String title);
	
	@Query("select b from Book b " +
			 "left join fetch b.author a " +
			 "left join fetch b.awards aw " +
			"where b.title like :title " +
			  "and b.bookType like :bookType " +
			  "and b.publishedDate between :fromPublishedDate and :toPublishedDate " +
			  "and a.id = :authorId " +
			  "and aw.id = :awardId")
	public Page<Book> findByCriteria(@Param("title") String bookTitle, 
			                         @Param("bookType") String bookType, 
                                     @Param("fromPublishedDate") Date fromPublishedDate, 
                                     @Param("toPublishedDate") Date toPublishedDate,
			                         @Param("authorId") BigInteger authorId, 
			                         @Param("awardId") BigInteger awardId, 
			                         Pageable pageable);

}